import java.util.concurrent.TimeUnit;

/**
 * @author welsir
 * @version 1.0
 * @description: TODO
 * @date 2022/10/17 8:51
 */

public class test {

    public static void main(String[] args) {

        Object lock = new Object();
        new Thread(() -> {
            synchronized (lock) {
                try {
                    System.out.println("线程" + Thread.currentThread().getName() + "正在等待锁...");
                    System.out.println("线程" + Thread.currentThread().getName() + "已经拿到锁...");
                    System.out.println("线程" + Thread.currentThread().getName() + "执行wait方法");
                    lock.wait();
                    System.out.println("线程" + Thread.currentThread().getName() + "wait end...");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "a").start();

        new Thread(() -> {
            System.out.println("线程" + Thread.currentThread().getName() + "正在等待锁...");
            synchronized (lock) {
                try {
                    System.out.println("线程" + Thread.currentThread().getName() + "已经拿到锁...");
                    lock.notify();
                    System.out.println("线程" + Thread.currentThread().getName() + "执行notify方法");
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println("线程" + Thread.currentThread().getName() + "执行sleep方法");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}